package com.lucanjun.cn.springboot.common.config;

import com.lucanjun.cn.springboot.common.constants.RequestHeaderConstant;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lucanjun
 * @version 1.0.0
 * @title Knife4jConfiguration
 * @create 2024-12-19 16:55:19
 * @description 接口文档配置类
 */
@Configuration
public class Knife4jConfiguration {
    @Bean
    public GroupedOpenApi AllApi(OperationCustomizer operationCustomizer) {      // 创建了一个api接口的分组
        return GroupedOpenApi.builder()
                .group("AllApi")         // 分组名称
                .pathsToMatch("/api/**")  // 接口请求路径规则
                .addOperationCustomizer(operationCustomizer)
                .build();
    }

    @Bean
    public GroupedOpenApi MasterDataSourceApi(OperationCustomizer operationCustomizer) {      // 创建了一个api接口的分组
        return GroupedOpenApi.builder()
                .group("测试模块")         // 分组名称
                .pathsToMatch("/api/test/**")  // 接口请求路径规则
                .addOperationCustomizer(operationCustomizer)
                .build();
    }

    @Bean
    public OperationCustomizer operationCustomizer() {
        return (operation, handlerMethod) -> {
            operation.addParametersItem(
                    new Parameter()
                            .in("header")
                            .required(true)
                            .description("登录用户token")
                            .name(RequestHeaderConstant.TOKEN)
                            .example(RequestHeaderConstant.TOKEN_VALUE));

            operation.addParametersItem(
                    new Parameter()
                            .in("header")
                            .required(true)
                            .description("登录用户名称")
                            .name(RequestHeaderConstant.USER_NAME)
                            .example(RequestHeaderConstant.USER_NAME_VALUE));
            return operation;
        };
    }

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("Knife4j标题")
                        .description("Knife4j说明")
                        .version("v1")
                        .contact(new Contact().name("robin").email("robin@gmail.com"))
                        .license(new License().name("Apache 2.0").url("http://springdoc.org"))
                );

    }

}
